package com.sample.camerax

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.loyal.camerax.utils.ActivityHelper.hideSystemUI
import com.loyal.camerax.utils.KEY_EVENT_ACTION
import com.loyal.camerax.utils.KEY_EVENT_EXTRA
import com.sample.camerax.databinding.ActivitySampleBinding

class SampleActivity : AppCompatActivity() {
    private val binding by lazy { ActivitySampleBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
    }
    override fun onResume() {
        super.onResume()
        binding.root.postDelayed({
            hideSystemUI()
        }, IMMERSIVE_FLAG_TIMEOUT)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return when (keyCode) {
            KeyEvent.KEYCODE_VOLUME_DOWN -> {
                val intent = Intent(KEY_EVENT_ACTION).apply { putExtra(KEY_EVENT_EXTRA, keyCode) }
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
                true
            }

            else -> super.onKeyDown(keyCode, event)
        }
    }

    companion object {
        private const val IMMERSIVE_FLAG_TIMEOUT = 500L
    }
}
